/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.mrp.ppm.entity;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import merlion.common.util.Consts;
import merlion.scm.lcm.entity.DeliveryOrder;

/**
 *
 * @author Zhang Ying, Zhu Xin
 */
@Entity
public class BackOrder implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @OrderBy("index")
    private ArrayList<IndexNValue> quantities;
    
    private String backOrderStrID;
    
    @OrderBy("orderedIndex")
    @OneToMany(targetEntity=IndexNDate.class, cascade={CascadeType.MERGE}, fetch=FetchType.EAGER)
    private List<IndexNDate> dates = new ArrayList<IndexNDate>();
    
    @OneToMany(targetEntity=BackOrderIndiv.class, cascade={CascadeType.MERGE}, fetch=FetchType.EAGER)
    private List<BackOrderIndiv> indivBo = new ArrayList<BackOrderIndiv>(); //0->A, 1->B,2->C,3->D
    
    private Timestamp dateCreated;
    
    @OneToOne(targetEntity=DeliveryOrder.class, cascade={CascadeType.MERGE}, fetch=FetchType.EAGER)
    private DeliveryOrder deliveryOrder;

    public BackOrder() {
        this.dateCreated = new Timestamp(System.currentTimeMillis());
        this.backOrderStrID = "BO" + Consts.sdfDmy.format(dateCreated);
        quantities = new ArrayList<IndexNValue>();
        for (int i = 0; i < 4; i++){
            IndexNValue temp = new IndexNValue(i, new Long(0));
            quantities.add(temp);
        }
    }
    
    public BackOrderIndiv returnBackOrderIndiv(int i) {
        return indivBo.get(i);
    }

    public List<IndexNDate> getDates() {
        return dates;
    }

    public void setDates(List<IndexNDate> dates) {
        this.dates = dates;
    }

    public List<BackOrderIndiv> getIndivBo() {
        return indivBo;
    }

    public void setIndivBo(List<BackOrderIndiv> indivBo) {
        this.indivBo = indivBo;
    }

    public String getBackOrderStrID() {
        return backOrderStrID;
    }

    public void setBackOrderStrID(String backOrderStrID) {
        this.backOrderStrID = backOrderStrID;
    }

    
    
    public DeliveryOrder getDeliveryOrder() {
        return deliveryOrder;
    }

    public void setDeliveryOrder(DeliveryOrder deliveryOrder) {
        this.deliveryOrder = deliveryOrder;
    }

    public Timestamp getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Timestamp dateCreated) {
        this.dateCreated = dateCreated;
    }

    public ArrayList<IndexNValue> getQuantities() {
        return quantities;
    }

    public void setQuantities(ArrayList<IndexNValue> quantities) {
        this.quantities = quantities;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof BackOrder)) {
            return false;
        }
        BackOrder other = (BackOrder) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.mrp.ppm.entity.BackOrder[ id=" + id + " ]";
    }
    
}
